package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.persist.entity.ArticleVideo;

public interface ArticleVideoMapper {
	
	@Delete(
			"delete from `article_video` where article_id = #{articleId, jdbcType=INTEGER}"
	)
	int deleteByArticleId(@Param("articleId")int articleId);
	
	 @Insert({
		"insert into article_video (article_id, video_id, sort, del, create_at, update_at, language)",
		"values (#{articleId,jdbcType=INTEGER}, #{videoId,jdbcType=INTEGER},#{sort,jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, ",
		"now(), now(), #{language,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(ArticleVideo record);
	    
	@Select({
	    "select",
	    "id, article_id, video_id, sort, del, create_at, update_at, language ",
	    "from article_video"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="video_id", property="videoId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleVideo> selectAll();
	
	@Select({
	    "select",
	    "id, article_id, video_id, sort, del, create_at, update_at, language ",
	    "from article_video",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="video_id", property="videoId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleVideo> selectByArticleId(@Param("articleId")int articleId, @Param("del")int del);
	
	@Select({
	    "select",
	    "id, article_id, video_id, sort, del, create_at, update_at, language ",
	    "from article_video ",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	ArticleVideo selectById(@Param("id") int id);
	
	@Select({
		"select",
	    "id, article_id, video_id, sort, del, create_at, update_at, language ",
	    "from article_video ",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="video_id", property="videoId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleVideo> selectByArticleIdAndDel(@Param("articleId")int articleId, @Param("del")int del);
	
	@Update({
	     "update article_video",
	     "set article_id = #{articleId,jdbcType=INTEGER},",
		     "video_id = #{videoId,jdbcType=INTEGER},",
		     "sort = #{sort,jdbcType=INTEGER},",
		     "del = #{del,jdbcType=INTEGER},",	
		     "create_at = #{createAt, jdbcType=TIMESTAMP},",
		     "update_at = #{updateAt,jdbcType=TIMESTAMP},",
		     "language = #{language,jdbcType=VARCHAR}",
	     "where Id = #{id,jdbcType=INTEGER}"
	 })
	 int updateByPrimaryKey(ArticleVideo record);	
}
